package com.ordering.action.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;


import com.ordering.action.BaseAction;
import com.ordering.constant.ResultResponse;
import com.ordering.entity.OrderItem;
import com.ordering.service.order.OrderItemService;
import com.ordering.service.order.impl.OrderItemServiceImpl;

public class MallAction extends BaseAction{
	
	OrderItemService orderItemService = new OrderItemServiceImpl();
	
	private List<OrderItem> OrderItemList;
	private OrderItem orderItem;
	private String jsonResult;
	private Integer productId;
	private Integer itemId;

	public String index(){
		return "index";
	}

	public String list(){
        try
        {
        	ResultResponse re = new ResultResponse();
        	OrderItem item = new OrderItem();
        	//根据用户id那购物车商品
        	item.setUserId(getUserInfo().getId());
        	item.setStatus(OrderItem.STATUS_CART);
        	re = orderItemService.getAll(item);
        	if(re.getCode() == ResultResponse.CODE_SUCCESS){
        		OrderItemList = (List<OrderItem>)re.getResult();
        	}else{
        		throw new Exception(re.getMessage());
        	}
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return FAIL;
        }
        return "list";
    }
	
	public void add(){
        try
        {
        	JSONObject jsonObject =  new JSONObject();
        	jsonObject.put("status",true);
        	
        	ResultResponse re = new ResultResponse();
        	
        	OrderItem item = new OrderItem();
        	item.setProductId(productId);
        	item.setUserId(getUserInfo().getId());
        	item.setStatus(OrderItem.STATUS_CART);
        	re = orderItemService.add(item);
        	if(re.getCode() != ResultResponse.CODE_SUCCESS){
        		jsonObject.put("status",false);
            	jsonObject.put("message",re.getMessage());
            	
        		throw new Exception(re.getMessage());
        	}
        	
        	outPutMessage(jsonObject.toString());
        }
        catch (Exception e)
        {
        	
            e.printStackTrace();
        }
        
        
    }
	
	public void delete(){
		JSONObject jsonObject = new JSONObject();
        try
        {
        	ResultResponse re = new ResultResponse();
        	jsonObject.put("status",true);
        	OrderItem item = new OrderItem();
        	item.setId(itemId);
        	re = orderItemService.delect(item);
        	if(re.getCode() != ResultResponse.CODE_SUCCESS){
        		jsonObject.put("status",false);
        		jsonObject.put("message",re.getMessage());
        		throw new Exception(re.getMessage());
        	}
        	outPutMessage(jsonObject.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
	
	
	
	
	private void outPutMessage(String jsonStr) throws IOException{
	    String contentType = "text/html;charset=utf-8";
		//指定输出内容类型和编码  
        ServletActionContext.getResponse().setContentType(contentType);   
        //获取输出流，然后使用  
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.print(jsonStr);
        out.flush();  
        out.close(); 
	}
	
	
	public List<OrderItem> getOrderItemList() {
		return OrderItemList;
	}

	public void setOrderItemList(List<OrderItem> orderItemList) {
		OrderItemList = orderItemList;
	}
	
	public OrderItem getOrderItem() {
		return orderItem;
	}

	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public Integer getItemId() {
		return itemId;
	}

	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}
}
